package com.novacloud.service.impl;

import com.github.pagehelper.PageHelper;
import com.novacloud.dao.TUserMapper;
import com.novacloud.domain.TUser;
import com.novacloud.domain.TUserExample;
import com.novacloud.service.UserService;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version 1.0
 * @date 2017/9/22 10:17
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private TUserMapper tUserMapper;

    @Override
    public List<TUser> findAll() {
        PageHelper.startPage(1, 5);
        List<TUser> list = tUserMapper.selectByExample(new TUserExample());
        return list;
    }

    @Override
    public int add(TUser tUser) {
        return tUserMapper.insert(tUser);
    }

    @Override
    public TUser getByUsername(String username) {
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andUsernameEqualTo(username);
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        if (!tUsers.isEmpty() && tUsers.size() == 1) {
            return tUsers.get(0);
        } else {
            throw new IncorrectResultSizeDataAccessException(1, tUsers.size());
        }
    }
}
